﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.Xml;

namespace Comm
{
    /// <summary>
    /// SEO实体类
    /// </summary>
    public class MSEO
    {
        private string _gID;
        private string _lookExpression;
        private string _metaKeywords;
        private string _pageTitle;
        private string _metaDescription;
        private int _ordinal;
        private bool _enabled;

        /// <summary>
        /// 主键
        /// </summary>
        public string GID
        {
            set { _gID = value; }
            get { return _gID; }
        }

        /// <summary>
        /// 匹配正则
        /// </summary>
        public string LookExpression
        {
            set { _lookExpression = value; }
            get { return _lookExpression; }
        }

        /// <summary>
        /// 关键词
        /// </summary>
        public string MetaKeywords
        {
            set { _metaKeywords = value; }
            get { return _metaKeywords; }
        }

        /// <summary>
        /// 标题
        /// </summary>
        public string PageTitle
        {
            set { _pageTitle = value; }
            get { return _pageTitle; }
        }

        /// <summary>
        /// 描述
        /// </summary>
        public string MetaDescription
        {
            set { _metaDescription = value; }
            get { return _metaDescription; }
        }

        /// <summary>
        /// 排序
        /// </summary>
        public int Ordinal
        {
            set { _ordinal = value; }
            get { return _ordinal; }
        }

        /// <summary>
        /// 启用
        /// </summary>
        public bool Enabled
        {
            set { _enabled = value; }
            get { return _enabled; }
        }
    }

    /// <summary>
    /// SEO操作类
    /// </summary>
    public class BSEO
    {
        private string path = HttpContext.Current.Server.MapPath("/App_Data/SEOSettingsInfo.xml");
        /// <summary>
        /// 获取集合
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public List<MSEO> getList(string strWhere)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(path);
            if (ds.Tables.Count < 1)
            {
                return null;
            }

            DataRow[] rows = ds.Tables[0].Select(strWhere);

            if (rows == null || rows.Length < 1)
            {
                return null;
            }

            List<MSEO> list = DataRowToList(rows);
            list.Sort(SortOrdinal);
            return list;
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="a1"></param>
        /// <param name="a2"></param>
        /// <returns></returns>
        private int SortOrdinal(MSEO a1, MSEO a2)
        {
            return a1.Ordinal.CompareTo(a2.Ordinal);
        }


        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="GID"></param>
        /// <returns></returns>
        public MSEO getModel(string GID)
        {
            List<MSEO> list = getList("GID='" + GID + "'");
            if (list == null || list.Count < 1)
            {
                return null;
            }
            return list[0];
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        public void Add(MSEO model)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);

            XmlNode SeoUrlRegularNode = doc.CreateElement("SeoUrlRegular");
            XmlNode GIDNode = doc.CreateElement("GID");
            GIDNode.InnerText=model.GID;
            XmlNode LookExpressionNode = doc.CreateElement("LookExpression");
            LookExpressionNode.InnerText = model.LookExpression;
            XmlNode MetaKeywordsNode = doc.CreateElement("MetaKeywords");
            MetaKeywordsNode.InnerText = model.MetaKeywords;
            XmlNode PageTitleNode = doc.CreateElement("PageTitle");
            PageTitleNode.InnerText = model.PageTitle;
            XmlNode MetaDescriptionNode = doc.CreateElement("MetaDescription");
            MetaDescriptionNode.InnerText = model.MetaDescription;
            XmlNode OrdinalNode = doc.CreateElement("Ordinal");
            OrdinalNode.InnerText = model.Ordinal.ToString();
            XmlNode EnabledNode = doc.CreateElement("Enabled");
            EnabledNode.InnerText = model.Enabled.ToString();

            SeoUrlRegularNode.AppendChild(GIDNode);
            SeoUrlRegularNode.AppendChild(LookExpressionNode);
            SeoUrlRegularNode.AppendChild(MetaKeywordsNode);
            SeoUrlRegularNode.AppendChild(PageTitleNode);
            SeoUrlRegularNode.AppendChild(MetaDescriptionNode);
            SeoUrlRegularNode.AppendChild(OrdinalNode);
            SeoUrlRegularNode.AppendChild(EnabledNode);

            doc["SeoUrlRegulars"].AppendChild(SeoUrlRegularNode);
            doc.Save(path);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        public void Update(MSEO model)
        {
            Delete(model.GID);
            Add(model);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        public void UpdateEnable(bool Enabled, string GID)
        {
           XmlDocument doc = XMLHelper.xmlDoc(path);
           XmlNodeList nodelist = doc["SeoUrlRegulars"].ChildNodes;
           foreach (XmlNode node in nodelist)
           {
               if (node["GID"].InnerText.Trim() == GID)
               {
                   node["Enabled"].InnerText = Enabled.ToString();
                   break;
               }
           }

           doc.Save(path);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        public void Delete(string GID)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);
            XmlNode node = null;
            foreach (XmlNode nd in doc["SeoUrlRegulars"].ChildNodes)
            {
                if (nd.Name == "SeoUrlRegular" && nd.ChildNodes[0].InnerText == GID)
                {
                    node = nd;
                }
            }
            if (node == null)
            {
                return;
            }
            doc["SeoUrlRegulars"].RemoveChild(node);
            doc.Save(path);
        }

        /// <summary>
        /// 将DataSet转成List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        private List<MSEO> DataSetToList(DataSet ds)
        {
            if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }

            List<MSEO> list = new List<MSEO>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                MSEO model = new MSEO();
                model.GID = row["GID"].ToString().Trim();
                model.LookExpression = row["LookExpression"].ToString().Trim();
                model.MetaKeywords = row["MetaKeywords"].ToString().Trim();
                model.PageTitle = row["PageTitle"].ToString().Trim();
                model.MetaDescription = row["MetaDescription"].ToString().Trim();
                model.Ordinal = Convert.ToInt32(row["Ordinal"]);
                model.Enabled = Convert.ToBoolean(row["Enabled"]);

                list.Add(model);
            }

            return list;
        }

        /// <summary>
        /// 将DataSet转成List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        private List<MSEO> DataRowToList(DataRow[] rows)
        {
            if (rows == null || rows.Length < 1)
            {
                return null;
            }

            List<MSEO> list = new List<MSEO>();
            foreach (DataRow row in rows)
            {
                MSEO model = new MSEO();
                model.GID = row["GID"].ToString().Trim();
                model.LookExpression = row["LookExpression"].ToString().Trim();
                model.MetaKeywords = row["MetaKeywords"].ToString().Trim();
                model.PageTitle = row["PageTitle"].ToString().Trim();
                model.MetaDescription = row["MetaDescription"].ToString().Trim();
                model.Ordinal = Convert.ToInt32(row["Ordinal"]);
                model.Enabled = Convert.ToBoolean(row["Enabled"]);

                list.Add(model);
            }

            return list;
        }
    }
}
